package cn.school.service;
import cn.school.dao.SubjectMapper;
import cn.school.entity.Subject;
import cn.school.util.MybatisUtil;
import cn.school.vo.Page;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
public class SubjectService {
    public static final Logger Log= LoggerFactory.getLogger(SubjectService.class);
    public static SubjectService INST=new SubjectService();

    public List<Subject> findALL(){
        SqlSession s= MybatisUtil.get();
        try {
            return s.getMapper(SubjectMapper.class).findALL();
        }finally {
            MybatisUtil.close(s);
        }
    }
    public int add(Subject subject){
        SqlSession s= MybatisUtil.get();
        try {
            return s.getMapper(SubjectMapper.class).add(subject);
        }finally {
            MybatisUtil.close(s);
        }
    }
    public int update(Subject subject){
        SqlSession s= MybatisUtil.get();
        try {
            return s.getMapper(SubjectMapper.class).modify(subject);
        }finally {
            MybatisUtil.close(s);
        }
    }
    public int modify(Subject subject){
        SqlSession s= MybatisUtil.get();
        try {
            return s.getMapper(SubjectMapper.class).modify(subject);
        }finally {
            MybatisUtil.close(s);
        }
    }
    //根据id查找单个subject信息
    public Subject get(long id){
        SqlSession s= MybatisUtil.get();
        try {
            return s.getMapper(SubjectMapper.class).get(id);
        }finally {
            MybatisUtil.close(s);
        }
    }
    public int del(long id){
        SqlSession s= MybatisUtil.get();
        try {
            return s.getMapper(SubjectMapper.class).del(id);
        }catch (Exception e){
            Log.error(e.getMessage(),e);
            return 0;
        }finally {
            MybatisUtil.close(s);
        }
    }

    public int findTotal(){
        SqlSession s= MybatisUtil.get();
        try {
            return s.getMapper(SubjectMapper.class).findTotal();
        }finally {
            MybatisUtil.close(s);
        }
    }
    //search
    public Page<Subject> search(  int pageNo,  int pageSize,   String subjectname) {
        SqlSession sqlSession = MybatisUtil.get();
        try {
            SubjectMapper mapper = sqlSession.getMapper(SubjectMapper.class);
            List<Subject> search = mapper.search(pageNo, pageSize, subjectname);
            int total = mapper.searchTotal(subjectname);
            return new Page<Subject>(search, pageNo, total);
        } finally {
            MybatisUtil.close(sqlSession);
        }
    }
}
